<template>
  <el-row v-loading="loading">
    <h1>指标管理</h1>
    <el-col :span="24">
      <index-edit ref="indexEdit" v-on:save-finished="searchIndex"></index-edit>
      <el-row class="search-row">
        <el-col :span="12">
          <el-input placeholder="姓名" size="small" v-model="search.indexName">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchIndex()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="12" style="text-align: right">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            v-show="permission.add"
            @click="handleCreateIndex()"
            >添加</el-button
          >
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table
            :data="indexList"
            border
            size="mini"
            header-cell-class-name="header-row"
          >
            <el-table-column
              prop="indexId"
              label="序号"
              header-align="center"
              align="center"
            >
            </el-table-column>

            <el-table-column
              prop="indexName"
              label="指标名称"
              header-align="center"
            >
            </el-table-column>

            <el-table-column
              prop="leftCritical"
              label="正常参数值"
              header-align="center"
            >
              <template slot-scope="scope">
                {{ scope.row.leftCritical }}-{{ scope.row.rightCritical }}
              </template>
            </el-table-column>

            <el-table-column prop="company" label="单位" header-align="center">
            </el-table-column>

            <el-table-column
              prop="limits"
              label="针对性别"
              header-align="center"
            >
            </el-table-column>

            <el-table-column
              label="操作"
              header-align="center"
              align="center"
              width="260"
            >
              <template slot-scope="scope">
                <el-button
                  v-show="permission.edit"
                  type="primary"
                  size="mini"
                  @click="handleChangePassword(scope.$index, scope.row)"
                  >编辑</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!-- 分页模块 -->
      <el-row>
        <el-col :span="24">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="1"
            :page-sizes="[3, 5, 10]"
            :page-size="3"
            layout="total, sizes, prev, pager, next, jumper"
            :total="indexTotal"
          >
          </el-pagination>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import IndexEdit from "@/components/HealthIntervention/IndexManagement/indexEdit";
import {
  ListAllIndex,
  ListIndexById,
} from "@/api/HealthIntervention/indexManagement";

export default {
  data() {
    return {
      loading: false,
      permission: {
        add: true,
        edit: true,
        remove: true,
      },
      search: {
        indexName: "",
        offset: 1,
        limit: 3,
      },
      indexTotal: 0,
      indexList: [],
    };
  },
  methods: {
    searchIndex() {
      this.loading = true;
      ListAllIndex(this.search)
        .then((response) => {
          this.indexTotal = response.data.total;
          this.indexList = response.data.rows;
          this.loading = false;
        })
        .catch((error) => {
          console.log(error);
          this.loading = false;
        });
    },

    handleSizeChange(val) {
      this.search.limit = val;
      this.search.offset = 1;
      this.searchIndex();
    },
    handleCurrentChange(val) {
      this.search.offset = val;
      this.search.currentPage = val;
      this.searchIndex();
    },
    //添加
    async handleCreateIndex() {
      this.loading = true;
      try {
        this.$refs.indexEdit.$emit("openAddIndexDialog");
        this.loading = false;
      } catch (error) {
        this.outputError(error);
      }
    },
    //修改
    async handleChangePassword(index, row) {
      this.loading = true;
      try {
        let [allIndResp] = await Promise.all([ListIndexById(row.indexId)]);
        this.$refs.indexEdit.$emit("openEditIndexDialog", allIndResp.data);
        this.loading = false;
      } catch (error) {
        this.outputError(error);
      }
    },

    //初始化查询信息
    async pageInit() {
      this.loading = true;
      try {
        let [searchUserResp] = await Promise.all([ListAllIndex(this.search)]);
        this.indexList = searchUserResp.data.rows;
        this.indexTotal = searchUserResp.data.total;
        this.loading = false;
      } catch (error) {
        this.outputError(error);
      }
    },

    outputError(error) {
      console.log(error.response ? error.response : error.message);
      this.loading = false;
      this.$message({
        showClose: true,
        message: "出错了，请按F12查看浏览器日志。",
        type: "error",
      });
    },
  },
  components: { IndexEdit },
  created() {
    this.pageInit();
  },
};
</script>

<style lang="scss" scoped>
.search-row {
  padding-bottom: 8px;
  .el-input {
    width: 260px;
  }
}

.user-locked {
  color: red;
}
.user-not-locked {
  color: green;
}
</style>